<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      v-loading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="物资名称" prop="materialName">
            <el-input v-model="formData.materialName" placeholder="请输入物资名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物资类别" prop="categoryId">
            <el-select v-model="formData.categoryId" clearable>
              <el-option
                v-for="dict in materialCategory"
                :key="dict.id"
                :label="dict.categoryName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="物资规格" prop="materialSpecification">
            <el-input v-model="formData.materialSpecification" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="基本单位" prop="unit">
            <el-input v-model="formData.unit" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="条码" prop="barcode">
            <el-input v-model="formData.barcode" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单价" prop="price">
            <InputNumber v-model="formData.price" type="decimal" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="物资描述" prop="materialDescribe">
            <el-input v-model="formData.materialDescribe" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否停用" prop="disable">
            <el-checkbox v-model="formData.disable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="MaterialInfoEdit" lang="ts">
import * as MaterialInfoApi from '@/api/material/materialInfo'
import InputNumber from '@/components/InputNumber/src/InputNumber.vue'
import * as MaterialInfoModel from '@/model/material/info/MaterialInfo'
const { t } = useI18n()
const formRef = ref()
const formLoading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const materialCategory = ref()
const message = useMessage()
let formType = ''
const formData = ref({
  materialName: '',
  categoryId: '',
  materialSpecification: '',
  unit: '',
  disable: 0,
  price: 0,
  barcode: '',
  materialDescribe: ''
})
const formRules = reactive({
  materialName: [{ required: true, message: '物资名称不能为空', trigger: 'blur' }]
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  materialCategory.value = await MaterialInfoApi.listQueryCategory('')
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MaterialInfoApi.singleGet(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MaterialInfoModel.MaterialInfo
    if (formType === 'create') {
      await MaterialInfoApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await MaterialInfoApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    materialName: '',
    categoryId: '',
    materialSpecification: '',
    unit: '',
    disable: 0,
    price: 0,
    barcode: '',
    materialDescribe: ''
  }
  formRef.value?.resetFields()
}
</script>
